//
//  AccoutTextViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/14.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "AccoutTextViewController.h"
#import "Mothy.h"
#import "CoreDataHandle.h"
@interface AccoutTextViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)  UIBarButtonItem *buttonItem;
@property (strong, nonatomic) IBOutlet UIView *liftView;
@property (strong, nonatomic) IBOutlet UIView *rightView;

@property (strong, nonatomic) IBOutlet UITextField *moneyTextField;

@property (strong, nonatomic) IBOutlet UITextField *nameTextFiled;
@property (strong, nonatomic) IBOutlet UIImageView *liftImageView;
@property (strong, nonatomic) IBOutlet UIImageView *rightIamgeView;
@property (strong, nonatomic) IBOutlet UILabel *liftLabel;
@property (strong, nonatomic) IBOutlet UILabel *rightLabel;
@property(strong,nonatomic)NSString *string;
@end

@implementation AccoutTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"添加一笔";
    
    
    UIBarButtonItem *liftItem=[[UIBarButtonItem alloc]initWithTitle:@"取消"style:UIBarButtonItemStylePlain target:self action:@selector(liftItemActon:)];
    self.navigationItem.leftBarButtonItem=liftItem;
    
    
    self.buttonItem=[[UIBarButtonItem alloc]initWithTitle:@"保存"style:UIBarButtonItemStylePlain target:self action:@selector(buttonItem:)];
    self.navigationItem.rightBarButtonItem=self.buttonItem;
    self.buttonItem.enabled=NO; //不能在被点击
    self.nameTextFiled.delegate=self;
    self.moneyTextField.delegate=self;
    if (self.string==nil) {
        self.string=@"type_big_0";

    }
       self.nameTextFiled.keyboardType= UIKeyboardTypeDefault;
    self.moneyTextField.keyboardType=UIKeyboardTypeNumberPad;
    UITapGestureRecognizer *taypGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGRAction:)];
    [self.liftView addGestureRecognizer:taypGR];
    UITapGestureRecognizer *rightTapGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightTapGRAction:)];
    [self.rightView addGestureRecognizer:rightTapGR];
}
-(void)tapGRAction:(UITapGestureRecognizer*)sender{
    if ([self.liftImageView.image isEqual:[UIImage imageNamed:@"type_big_0"]]) {
        self.nameTextFiled.placeholder=@"给谁的:";
        self.liftImageView.image=[UIImage imageNamed:@"chose_business_normal@2x"];
        self.rightIamgeView.image=[UIImage imageNamed:@"type_big_24"];
        self.string=@"type_big_24";
    }else{
    self.nameTextFiled.placeholder=@"收谁的:";
    self.liftImageView.image=[UIImage imageNamed:@"type_big_0"];
    self.rightIamgeView.image=[UIImage imageNamed:@"chose_business_normal@2x"];
          self.string=@"type_big_0";
    }
}
-(void)rightTapGRAction:(UITapGestureRecognizer*)sender{
    if ([self.rightIamgeView.image isEqual:[UIImage imageNamed:@"type_big_24"]]) {
        self.nameTextFiled.placeholder=@"收谁的:";
        self.liftImageView.image=[UIImage imageNamed:@"type_big_0"];
        self.rightIamgeView.image=[UIImage imageNamed:@"chose_business_normal@2x"];
        self.string=@"type_big_0";
        
    }else{
    self.nameTextFiled.placeholder=@"给谁的:";
    self.liftImageView.image=[UIImage imageNamed:@"chose_business_normal@2x"];
    self.rightIamgeView.image=[UIImage imageNamed:@"type_big_24"];
    self.string=@"type_big_24";
    }
}

-(void)liftItemActon:(UIBarButtonItem*)sender{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(void)buttonItem:(UIBarButtonItem*)sender{
    Mothy *model=[NSEntityDescription insertNewObjectForEntityForName:@"Mothy" inManagedObjectContext:[CoreDataHandle shareCoreDataHandle].context];
    model.name=self.nameTextFiled.text;
    model.closed=self.moneyTextField.text;

        model.picture=self.string;
    //通过上面几步并没有真正的把aPension保存到数据库,而是暂时保存到了context里面
    //判断如果context 发生改变,则进行数据保存
    if ([[CoreDataHandle shareCoreDataHandle].context hasChanges]) {
        [[CoreDataHandle shareCoreDataHandle].context save:nil];
    }
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
   
    if ([self.nameTextFiled isEqual:textField]) {
         NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(self.moneyTextField.text.length>0&&toBeString.length>0){
            self.buttonItem.enabled=YES;
        }
    }
    if ([self.moneyTextField isEqual:textField]) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(self.nameTextFiled.text.length>0&&toBeString.length>0){
            self.buttonItem.enabled=YES;
        }
    }
    if ([self.nameTextFiled isEqual:textField]) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(self.moneyTextField.text.length>0&&toBeString.length==0){
            self.buttonItem.enabled=NO;
            ;
        }
    }
    if ([self.moneyTextField isEqual:textField]) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(self.nameTextFiled.text.length>0&&toBeString.length==0){
            self.buttonItem.enabled=NO;
        }
    }
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
